@php
    // 使用传入的 codeGenerator 数据
    $table = $codeGenerator['table'] ?? [];
    $packageName = strtolower($codeGenerator['module'] ?? '');
    $componentName = $table['pascalCase'] ?? '';
    $camelCaseName = $table['camelCase'] ?? '';
@endphp
/**
 * MineAdmin is committed to providing solutions for quickly building web applications
 * Please view the LICENSE file that was distributed with this source code,
 * For the full copyright and license information.
 * Thank you very much for using MineAdmin.
 *
 * @Author X.Mo<root@imoi.cn>
 * @Link   https://github.com/mineadmin
 */
import type { MaFormItem } from '@mineadmin/form'
import type { {{$componentName}}Vo } from '~/{{ $packageName }}/api/{{ $camelCaseName }}.ts'

export default function getFormItems(formType: 'add' | 'edit' = 'add', t: any, model: {{$componentName}}Vo): MaFormItem[] {
  // 新增默认值
  if (formType === 'add') {
@foreach($codeGenerator['formFields'] ?? [] as $field)
@if(isset($field['default']))
@if(in_array($field['dbType'], ['int', 'tinyint', 'smallint', 'bigint', 'decimal', 'float', 'double']))
    model.{{$field['field']}} = Number({{$field['default']}})
@else
    model.{{$field['field']}} = String('{{$field['default']}}')
@endif
@endif
@endforeach
  }

  // 编辑默认值
  if (formType === 'edit') {
    // todo...
  }

  return [
@foreach($codeGenerator['formFields'] ?? [] as $field)
@if($field['is_form'] ?? false)
@php
      $label = $field['label'];
      if (strpos($label, ':') !== false) {
        $parts = explode(':', $label, 2);
        $label = trim($parts[0]);
      }
      $renderComponent = $field['component'] ??'el-input';
      if($field['component'] == 'el-switch' || $field['component'] == 'el-select') {
        $renderComponent = 'ma-dict-select';
      } elseif($field['component'] == 'el-editor') {
        $renderComponent = 'el-input';
      }
@endphp
    {
      label: '{{$label}}', // t('{{$packageName . $componentName}}.{{$field['field']}}'),
      prop: '{{$field['field']}}',
      render: () => <{{$renderComponent}} />,
@if(isset($field['renderProps']))
      renderProps: {
@php
        $first = true;
        foreach ($field['renderProps'] as $key => $val) {
          if (!$first) {
            echo "\n        ";
          } else {
            echo "        ";
          }
          $first = false;
          
          // 键名不需要引号，除非包含特殊字符
          if (preg_match('/^[a-zA-Z_$][a-zA-Z0-9_$]*$/', $key)) {
            echo $key;
          } else {
            echo "'" . $key . "'";
          }
          
          echo ": ";
          
          // 根据值类型进行处理
          if (is_array($val)) {
            // 格式化数组以符合 ESLint 要求
            if (array_is_list($val)) {
              // 索引数组
              echo "[\n";
              foreach ($val as $index => $item) {
                echo "          ";
                if (is_array($item)) {
                  echo "{ ";
                  $itemPairs = [];
                  foreach ($item as $itemKey => $itemValue) {
                    $formattedKey = preg_match('/^[a-zA-Z_$][a-zA-Z0-9_$]*$/', $itemKey) ? $itemKey : "'$itemKey'";
                    if (is_string($itemValue)) {
                      $itemPairs[] = "$formattedKey: '$itemValue'";
                    } elseif (is_numeric($itemValue)) {
                      $itemPairs[] = "$formattedKey: $itemValue";
                    } elseif (is_bool($itemValue)) {
                      $itemPairs[] = "$formattedKey: " . ($itemValue ? 'true' : 'false');
                    } else {
                      $itemPairs[] = "$formattedKey: " . json_encode($itemValue, JSON_UNESCAPED_UNICODE|JSON_UNESCAPED_SLASHES);
                    }
                  }
                  echo implode(", ", $itemPairs) . " }";
                } else {
                  echo json_encode($item, JSON_UNESCAPED_UNICODE|JSON_UNESCAPED_SLASHES);
                }
                echo ",";
                echo "\n";
              }
              echo "        ]";
            } else {
              // 关联数组
              echo json_encode($val, JSON_UNESCAPED_UNICODE|JSON_UNESCAPED_SLASHES);
            }
          } elseif (is_bool($val)) {
            echo $val ? 'true' : 'false';
          } elseif (is_null($val)) {
            echo 'null';
          } elseif (is_numeric($val)) {
            echo $val;
          } elseif (is_string($val) && strpos($val, "t('") === 0) {
            echo $val;
          } else {
            echo "'" . str_replace("'", "\\'", $val) . "'";
          }
          echo ",";
        }
        echo "\n      ";
@endphp
},
@endif
@if(($field['required'] ?? false) || str_contains($field['field'], 'name'))
      itemProps: {
        rules: [{ required: true, message: t('form.pleaseInput', { msg: '{{$label}}' }) }],
      },
@endif
    },
@endif
@endforeach
  ]
}
